﻿var text = "";
var edit_id = "";
var edit_content = "";
var del_reply_id = "";
var del_cmt_id = "";
var edit_cmt_id = "";
var edit_cmt_content = "";
var edit_cmt_reply_id = "";
function chontrinhdo(evt, trinhdo) {
    var i, x, tablinks;
    x = document.getElementsByClassName("cer-content");
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("cer-link");
    for (i = 0; i < x.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" w3-white w3-border-teal", " w3-grey w3-border-grey");
    }
    document.getElementById(trinhdo).style.display = "block";
    evt.currentTarget.className = evt.currentTarget.className.replace(" w3-grey w3-border-grey", " w3-white w3-border-teal");
}
function get_input(id) {
    text = id;
}
function clear_input() {
    text = "";
}
function edit_answer(id) {
    edit_id = id;
}
function edit_cmt_input(id) {
    edit_cmt_id = id;
}
function clear_edit() {
    edit_id = "";
}
function clear_cmt_input() {
    edit_cmt_id = "";
}
$(document).ready(function () {
    $('[data-toggle="tooltip"]').tooltip();
});
$(document).ready(function () {
    $(document).keypress(function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            if (text != "") {
                var reply = "";
                if (text == '0') {
                    reply = document.getElementById("txt_reply").value;
                    if (reply.trim().length > 10) {
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                var temp = document.getElementById("cmt_area").innerHTML;
                                document.getElementById("cmt_area").innerHTML = xhttp.responseText;
                                document.getElementById("cmt_area").innerHTML += temp;
                                $(".faq-edit-answer-btn").click(function () {
                                    $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
                                    $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
                                    this.style.display = "none";
                                    $(this).parent().children(".faq-cancel-edit-answer-btn").animate({ height: "toggle" }, "fast");
                                })
                                $(".faq-cancel-edit-answer-btn").click(function () {
                                    $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
                                    $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
                                    this.style.display = "none";
                                    $(this).parent().children(".faq-edit-answer-btn").animate({ height: "toggle" }, "fast");
                                })
                                document.getElementById("txt_reply").value = "";
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/lesson/add_Comment.aspx?content=" + reply, true);
                        xhttp.send();
                    }
                    else {
                        alert("Câu hỏi phải tối thiểu 10 kí tự")
                    }
                }
                else {
                    reply = document.getElementById("cmt_" + text).getElementsByClassName("txt_answer")[0].value;
                    if (reply.trim() != "") {
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                document.getElementById("cmt_" + text).innerHTML = xhttp.responseText;
                                $(".faq-edit-answer-btn").click(function () {
                                    $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
                                    $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
                                    this.style.display = "none";
                                    $(this).parent().children(".faq-cancel-edit-answer-btn").animate({ height: "toggle" }, "fast");
                                })
                                $(".faq-cancel-edit-answer-btn").click(function () {
                                    $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
                                    $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
                                    this.style.display = "none";
                                    $(this).parent().children(".faq-edit-answer-btn").animate({ height: "toggle" }, "fast");
                                })
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/lesson/add_Comment_Reply.aspx?id=" + text + "&content=" + reply + "&edit_id=" + "&edit_content=", true);
                        xhttp.send();
                    }
                }
            }
            if (edit_id != "") {
                edit_content = document.getElementById("txt_answer_edit_" + edit_id).value;
                var xhttp;
                xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange = function () {
                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                        document.getElementById("cmt_" + text).innerHTML = xhttp.responseText;
                        $(".faq-edit-answer-btn").click(function () {
                            $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
                            $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
                            this.style.display = "none";
                            $(this).parent().children(".faq-cancel-edit-answer-btn").animate({ height: "toggle" }, "fast");
                        })
                        $(".faq-cancel-edit-answer-btn").click(function () {
                            $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
                            $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
                            this.style.display = "none";
                            $(this).parent().children(".faq-edit-answer-btn").animate({ height: "toggle" }, "fast");
                        })
                    }
                };
                xhttp.open("GET", "../ajax/asp/lesson/add_Comment_Reply.aspx?id=" + text + "&content=" + "&edit_id=" + edit_id + "&edit_content=" + edit_content, true);
                xhttp.send();
            }
            if (edit_cmt_id != "") {
                var edited = document.getElementById("cmt_" + edit_cmt_id).getElementsByClassName("txt_edit_cmt")[0].value;
                $.post("../Ajax/asp/Lesson/edit_Comment.aspx", { "id": edit_cmt_id, "content": edited });
                
                document.getElementById("cmt_" + edit_cmt_id).getElementsByClassName("cmt_Content")[0].innerHTML = edited;
            }
        }
    });
    $(".faq-edit-answer-btn").click(function () {
        $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
        $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
        this.style.display = "none";
        $(this).parent().children(".faq-cancel-edit-answer-btn").animate({ height: "toggle" }, "fast");
    })
    $(".faq-cancel-edit-answer-btn").click(function () {
        $(this).parent().parent().children(".faq-answer-content").animate({ height: "toggle" }, "fast");
        $(this).parent().parent().children(".faq-answer-content-edit").animate({ height: "toggle" }, "fast");
        this.style.display = "none";
        $(this).parent().children(".faq-edit-answer-btn").animate({ height: "toggle" }, "fast");
    })
    $(".usual-ques-btn").click(function () {
        $("#usual-ques-modal").fadeIn();
    })
    $(".usual-ques-item").click(function () {
        $(this).parent().children(".usual-ques-answer").animate({ height: "toggle" }, "fast");
    });

})

function show_answer(evt) {
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-older-answer")[0].style.display = "block";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("nut-hien")[0].style.display = "none";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("nut-an")[0].style.display = "block";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-teacher-reply")[0].style.display = "none";
}

function hide_answer(evt) {
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-older-answer")[0].style.display = "none";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("nut-hien")[0].style.display = "block";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("nut-an")[0].style.display = "none";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-teacher-reply")[0].style.display = "block";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-answer-row")[0].style.display = "none";
}
function answer_btn(evt) {
    //evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-teacher-reply")[0].style.display = "none";
    //evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-older-answer")[0].style.display = "block";
    evt.currentTarget.parentElement.parentElement.getElementsByClassName("faq-answer-row")[0].style.display = "block";
    //$(evt.currentTarget).parent().parent().find(".nut-hien").css("display", "none");
    //$(evt.currentTarget).parent().parent().find(".nut-an").css("display", "block");
}
function pin_btn(id) {
    $("#cmt_" + id).find(".faq-ghim-sign").removeClass("faq-ghim-sign-hide");
    $("#cmt_" + id).find(".faq-ghim-sign").addClass("faq-ghim-sign-show");
    document.getElementById("cmt_" + id).getElementsByClassName("faq-ghim-btn")[0].style.display = "none";
    $("#cmt_" + id).find(".faq-unghim-btn").animate({ height: "toggle" }, "fast");
    $.post("../ajax/asp/Lesson/pin.aspx", { "id": id });
}
function unpin_btn(id) {
    $("#cmt_" + id).find(".faq-ghim-sign").addClass("faq-ghim-sign-hide");
    $("#cmt_" + id).find(".faq-ghim-sign").removeClass("faq-ghim-sign-show");
    document.getElementById("cmt_" + id).getElementsByClassName("faq-unghim-btn")[0].style.display = "none";
    $("#cmt_" + id).find(".faq-ghim-btn").animate({ height: "toggle" }, "fast");
    $.post("../ajax/asp/Lesson/unpin.aspx", { "id": id });
}
function del_reply(id) {
    del_reply_id = id;

    document.getElementById("del_reply").style.display = "block";
}
function btn_del_reply() {
    $('#del_reply').fadeOut();
    $(".reply_" + del_reply_id).remove();
    $.post("../ajax/asp/lesson/del_Reply.aspx", { "id": del_reply_id });
}
function del_cmt(id) {
    del_cmt_id = id;
    document.getElementById("del_cmt").style.display = "block";
}
function btn_del_cmt() {
    $('#del_cmt').fadeOut();
    $("#cmt_" + del_cmt_id).remove();
    $.post("../ajax/asp/lesson/del_Cmt.aspx", { "id": del_cmt_id });
}

function edit_cmt(id) {
    var cmt = document.getElementById("cmt_" + id).getElementsByClassName("cmt_Content")[0].innerHTML;
    document.getElementById("cmt_" + id).getElementsByClassName("cmt_Content")[0].innerHTML = "<input type='text' class='txt_edit_cmt' onclick='edit_cmt_input(" + id + ")' onblur='clear_cmt_input()' value='" + cmt + "' />";
}